/**
 * 表单验证
 * @file
 * @date 2018-12-20 10:55:02
 */

layui.define(function (exports) {

    function mobileOrTel(value) {
        value = value.replace(/[^-|\d]/g, '');
        return /^1\d{10}$/.test(value) || /0\d{10}$/.test(value) || /^0\d{7,8}$/.test(value);
    }

    /**
     * 验证手机号
     * @param value
     * @returns {boolean}
     */
    function mobile (value) { var length = value.length; return (length == 11 && /^1[0-9]{8,20}$/.test(value)) }

    /**
     * 验证手机号
     * @param value
     * @returns {boolean}
     */
    function phone (value) { return  /^(13|14|15|16|17|18|19)\d{9}$/.test(value); }

    /**
     * 验证固话
     * @param value
     * @returns {boolean}
     */
    function tel (value) {
        //return  /^[0-9]{7,15}$/.test(value) || /^((\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)$/.test(value);
        return /^0\d{9,11}$/.test(value) || /^[1-9]\d{6,7}$/.test(value);
    }

    /**
     * 0、正整数
     * @param value
     * @returns {boolean}
     */
    function isInteger (value) { return !/[^0-9]/.test(value); }

    /**
     * 正整数
     * @param value
     * @returns {boolean}
     */
    function isPositiveInteger (value) { return /^[1-9][\d]*$/.test(value); }

    /**
     * 可保留两位小数的正数
     * @returns {boolean}
     */
    function isZDouble (value) { return /^(([0-9]+)|([0-9]+\.[0-9]{1,2}))$/.test(value); }

    var validate = {
        mobile: mobile,
        phone: phone,
        tel: tel,
        mobileOrTel: mobileOrTel,
        isPosInt: isPositiveInteger,
        isInteger: isInteger,
        isZDouble: isZDouble
    };

    exports('zmn_validate', validate);
});